class Solution:
    def minMalwareSpread(self, graph: List[List[int]], initial: List[int]) -> int:
        def dfs(u, s):
            nonlocal n
            if u in initial:
                return {}
            s.add(u)
            for v in range(n):
                if not graph[u][v] or v in s:
                    continue
                ns = dfs(v, s)
                if not ns:
                    return {}
            return s

        n = len(graph)
        initial.sort()
        mx = ans = -1
        for u in initial:
            vis = {u}
            for v in range(n):
                if graph[u][v]:
                    ns = dfs(v, {u})
                    vis = vis.union(ns)
            cur = len(vis)
            if cur > mx:
                mx = cur
                ans = u
        return ans